package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    //开启学生管理系统
    public void start() {

        lo:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您的使用！");
                    break lo;
                default:
                    System.out.println("您的输入有误，请重新输入");
            }
        }
    }
    //修改
    public void updateStudent() {
        String id = inputStudentId();
        Student st = inputStudentInfo(id);
        studentService.updateStudent(id, st);
        System.out.println("修改成功");
    }
    //删除
    public final void deleteStudent() {
        String id = inputStudentId();
        studentService.deleteStudentByld(id);
        System.out.println("删除成功");
    }
    //查看
    public final void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        System.out.println("学号 \t姓名 \t\t年龄 \t\t生日");
        if (stus == null) {
            System.out.println("查无信息，请重新输入");
            return;
        } else {
            for (int i = 0; i < stus.length; i++) {
                Student stu = stus[i];
                if (stu != null) {
                    System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t\t" + stu.getBirthday());
                }
            }
        }

    }
    //添加
    public final void addStudent() {

        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            boolean result1 = studentService.isExists(id);
            if (result1) {
                System.out.println("学号被占用，请重新输入");
            } else {
                break;
            }
        }

        Student st = inputStudentInfo(id);
        boolean result = studentService.addStudent(st);
        if (result) {
            System.out.println("添加成功");
        } else
            System.out.println("添加失败");

    }
    //键盘录入学生id
    public final String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入您要删除的学号");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("您输入的学号不存在,请重新输入");
            } else {
                break;
            }
        }
        return id;
    }

    public abstract Student inputStudentInfo(String id);
}
